class Solution
{
public:
    long long interchangeableRectangles(vector<vector<int>> &rectangles)
    {
        auto encodeShape = [](int w, int h)
        {
            return w * 100000LL + h;
        };
        unordered_map<long long, long long> shapeCount;
        for (auto &rectangle : rectangles)
        {
            int _gcd = gcd(rectangle[0], rectangle[1]);
            ++shapeCount[encodeShape(rectangle[0] / _gcd, rectangle[1] / _gcd)];
        }
        long long result = 0;
        for (auto &[_, count] : shapeCount)
        {
            result += count * (count - 1) / 2;
        }
        return result;
    }
};